package com.evl.wxpay.util.paytype;

import java.util.Map;

import com.evl.wxpay.util.WXPay;
import com.evl.wxpay.util.WXPayConfig;
import com.evl.wxpay.util.WXPayConfigImpl;

/**
 * 支付请求
 * @author dangsheng
 *
 */
public class WxPayApi {
	
	private WXPay wxpay;
	
	private static WxPayApi INSTANCE;
	
	WxPayApi() throws Exception{
		WXPayConfig config = WXPayConfigImpl.getInstance();
		wxpay = new WXPay(config, false, false);
	}
	
	/**
	 * 单例模式,wxpay只初始化一次
	 * @return
	 * @throws Exception
	 */
	public static WxPayApi Instance() throws Exception{
        if (INSTANCE == null) {
            synchronized (WxPayApi.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WxPayApi();
                }
            }
        }
        return INSTANCE;
	}
	
	/**
	 * 发起支付请求
	 * @param map
	 * @param payType
	 * @return
	 * @throws Exception
	 */
	public Map request(Object obj,PayType payType){
		try {
			payType.initData(obj).request(wxpay).record();
		} catch (InsertPayRecordException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return payType.getRspData();
	}
}
